//
//  UIImage+BAContact.m
//  BAContact
//
//  Created by boai on 2017/8/3.
//  Copyright © 2017年 boai. All rights reserved.
//

#import "UIImage+BAContact.h"

@implementation UIImage (BAContact)

/*!
 获得新的圆形图片
 
 @param image 传入需要裁剪成圆形的图片
 @return 返回裁剪好的圆形图片
 */
+ (UIImage *)ba_imageToRoundImageWithImage:(UIImage *)image
{
    if (!image)return nil;
    
    /*! 1、开启位图上下文 */
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0) ;
    
    /*! 2、描述圆形裁剪区域 */
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)] ;
    
    /*! 3、设置裁剪区域 */
    [clipPath addClip] ;
    
    /*! 4、绘图 */
    [image drawAtPoint:CGPointZero] ;
    
    /*! 5、取出图片 */
    image = UIGraphicsGetImageFromCurrentImageContext() ;
    
    /*! 6、关闭图片上下文 */
    UIGraphicsEndImageContext() ;
    
    return image ;
}

/*!
 *  传入图片，cell，imageSize，改变自定义大小的圆形系统 cell image
 *
 *  @param cell      cell
 *  @param image     image 可以自己切圆角：[UIImage ba_imageToRoundImageWithImage:UIImageMake(@"icon1.jpg")]；
 *  @param imageSize 图像的 size 记得先切圆角再传 size
 *
 *  @return 返回自定义大小的圆形系统 cell image
 */
+ (UIImage *)ba_imageToChangeCellRoundImageViewSizeWithCell:(UITableViewCell *)cell
                                                      image:(UIImage *)image
                                                  imageSize:(CGSize)imageSize
{
    UIImage *roundImage = [UIImage ba_imageToRoundImageWithImage:image];
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    [roundImage drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return cell.imageView.image;
}

@end

@implementation NSString (BAContact)


/**
 判断 string 是否为空
 
 @return YES：为空，NO：不为空
 */
- (BOOL)ba_stringIsBlank
{
    NSCharacterSet *blank = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    for (NSInteger i = 0; i < self.length; ++i) {
        unichar c = [self characterAtIndex:i];
        if (![blank characterIsMember:c]) {
            return NO;
        }
    }
    return YES;
}

/**
 判断一个字符串里面是否包含另一个字符串
 
 @param string string
 @return YES/NO
 */
- (BOOL)ba_stringIncludesString:(NSString *)string
{
    if ([string ba_stringIsBlank])
    {
        return NO;
    }
    
    if ([self respondsToSelector:@selector(containsString:)])
    {
        return [self containsString:string];
    }
    
    return [self rangeOfString:string].location != NSNotFound;
}
@end
